package com.cx.base.day23_Redis.jedis;

import redis.clients.jedis.Jedis;

import java.util.Set;

/**
 * @Author: songyahui
 * @Date: 2020/7/20 19:06
 * @Version 1.0
 */
public class RedisDemo01 {
    public static void main(String[] args) {
//        创建连接
        Jedis jedis = new Jedis("127.0.0.1", 6379);
//        密码
//        jedis.auth("");
//        数据库
        jedis.select(5);
        //连接测试
        System.out.println(jedis.ping());
        //查看数据库
        System.out.println(jedis.getDB());
        System.out.println("情况数据：" + jedis.flushDB());
        System.out.println("判断键是否存在：" + jedis.exists("username"));
        System.out.println("新增username,song的键值对：" + jedis.set("username", "song"));
        System.out.println("新组password,password键值对：" + jedis.set("password", "password"));
        System.out.println("系统中所有的键如下：");
        Set<String> keys = jedis.keys("*");
        System.out.println(keys);
        System.out.println("删除键password:" + jedis.del("password"));
        System.out.println("判断password是否存在：" + jedis.exists("password"));
        System.out.println("查看username所存储的值的类型：" + jedis.type("username"));
        System.out.println("随机返回key空间的一个：" + jedis.randomKey());
        System.out.println("重命名key:" + jedis.rename("username", "name"));
        System.out.println("取出改后的name:" + jedis.get("name"));
        System.out.println("按索引查询：" + jedis.select(0));
        System.out.println("删除当前选择数据库中的所有key:" + jedis.flushDB());
        System.out.println("返回当前数据库中key的数目：" + jedis.dbSize());
        System.out.println("删除所有数据库中的所有key:" + jedis.flushAll());
//        hash
        jedis.hset("user:1", "name", "Jack");
        jedis.hset("user:2", "name", "Tom");
        System.out.println(jedis.hexists("user:2", "name"));
    }
}
